package com.shrios;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

@Component
public class UserRealm extends AuthorizingRealm {

    @Override
    public void setName(String name) {
        super.setName("myRelam");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权逻辑");

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

//        String tol = (String) principalCollection.getPrimaryPrincipal();

//        System.out.println(tol);
        info.addStringPermission("user:add");
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证逻辑");

        String name = "admin";
        String pwd = "123";

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        System.out.println((String) token.getPrincipal());

        String a = (String) token.getPrincipal();
        char[] p = token.getPassword();
        System.out.println(a + ":" + new String(p));
        if (!token.getUsername().equals(name)) {
            return null;
        }

        //------org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY:this is what --------
        // 把用户的认证所带有的逻辑存在session里面，俗称principle credit
        return new SimpleAuthenticationInfo("user:add", pwd, getName());
    }
}
